package com.ketai.service.impl;

import cn.hutool.core.util.StrUtil;
import com.ketai.common.MessageConstant;
import com.ketai.common.StatusCode;
import com.ketai.common.KetaiException;
import com.ketai.dao.OrganizationDao;
import com.ketai.dao.impl.OrganizationDaoImpl;
import com.ketai.pojo.Organization;
import com.ketai.service.OrganizationService;
import com.ketai.util.Pages;
import com.ketai.vo.ActivityVo;

import javax.naming.NamingException;
import javax.xml.stream.events.StartDocument;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: 高伟华
 * @Description:
 * @Date:Create In 2022-08-06 15:27
 **/
public class OrganizationServiceImpl implements OrganizationService {

    OrganizationDao organizationDao =new OrganizationDaoImpl();


    @Override
    public List<Organization> queryOrganizationList() {
        try {
          return   organizationDao.queryOrganizationList();
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (NamingException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public int deleteOrganizationById(Integer id)  {
        try {
            organizationDao.deleteOrganizationById(id);
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (NamingException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int addOrganization(Organization organization)throws KetaiException {
        int result = 0;
        try {
           result= organizationDao.addOrganization(organization);
        } catch (SQLException e) {
            e.printStackTrace();
            //全局异常  添加失败
            throw new KetaiException(StatusCode.ERROR, MessageConstant.MECHANISM_SAVE_ERROR);
        } catch (NamingException e) {
            e.printStackTrace();
        }
        return result;
    }

    @Override
    public Organization organizationById(String id) {
        try {
         return organizationDao.organizationById(id);
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (NamingException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public int checkedorganization(String id, String open, String desc, String pid) {
        String sql  ="update organizationbase set auditing=?";
        List params = new ArrayList();
        if(!StrUtil.isBlank(open)&&open.equals("on")){
            params.add(1);
        }else{
            sql+=" , `desc`=? ";
            params.add(2);
            params.add(desc);
        }
        sql+=" ,Created=now(),ocheckId=? where id=?";
        params.add(pid);
        params.add(id);
        System.out.println(sql);
        try {
          return  organizationDao.checkedOrganization(sql,params.toArray());
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (NamingException e) {
            e.printStackTrace();
        }
        return 0;
    }


}
